<template>
	<view>
		<view class="top-head">
			<view class="screen-bg">
				<block v-for="(item, index) in screen_list">
					<view class="screen-item" :class="{choose: current_index == index}" @click="onScreen(index)">{{item.title}}</view>
				</block>
			</view>
			
			<scroll-view class="wrap-tab" scroll-x :bounces="false" :show-scrollbar="false"  v-if="current_index == 0">
				<block v-for="(item, index) in category_list" :key="index">
					<view class="tab-item" :class="category_id == item.id ? 'item-select' : ''" @click="onTab(item)">{{item.name}}</view>
				</block>
			</scroll-view>
		</view>
		
		<view class="wrap">
			<view class="wrap-list">
				<block v-for="(item, index) in list">
					<view class="item" @click="onDetail(item)">
						<image class="bg" :src="item.live_image"></image>
						
						<view class="content">
							<view class="status-bg">
								<image src="@/static/live.png" class="pic"></image>
								
								<view class="status" v-if="item.status == 0">直播中</view>
								<view class="status" v-else-if="item.status == 1">直播结束</view>
								<view class="status" v-else-if="item.status == 2">直播封禁</view>
							</view>
							
							
							<view class="bottom">
								<view class="title">{{item.title}}</view>
								
								<view class="info">
									<image class="avatar" :src="item.user.avatar"></image>
									<view class="nickname">{{item.user.nickname}}</view>
									<view class="num">{{item.look_num | liveLookNum}}人 在看</view>
								</view>
							</view>
						</view>
					</view>
				</block>
			</view>
			
			<!-- <u-empty text="暂无数据" mode="list" v-if="list.length == 0"></u-empty> -->
			<abnor v-if="list.length<=0"></abnor>
		</view>
		
		<tabbar :cur="8"></tabbar>
	</view>
</template>

<script>
	import {
		liveCategoryList, //直播分类
		liveList, //直播列表
	} from "@/api/newLive.js";
	import tabbar from "@/components/tabbar.vue";
	import storage from "@/utils/storage.js";
	import AppSocket from "@/utils/webSocket.js";

	export default {
		components: {
			tabbar
		},
		data() {
			return {
				category_list: [],
				category_id: 0,
				
				screen_list: [
					{title: "推荐"},
					{title: "关注"},
				],
				current_index: 0,
				
				type: 2,
				
				page: 1,
				total: 0,
				list: [],
			}
		},
		onLoad(options) {
			if (options.type) {
				this.type = options.type
			}
			this.getCategoryList()
		},
		onShow() {
			this.onRefresh()
		},
		/**
		 * 刷新
		 */
		onPullDownRefresh() {
			this.onRefresh()
		},
		/**
		 * 加载更多
		 */
		onReachBottom() {
			if (this.list.length < this.total) {
				this.page++
				this.getLiveList()
			}
		},
		/**
		 * 方法
		 */
		methods: {
			/**
			 * 选择分类
			 */
			onTab(item) {
				this.category_id = item.id
				this.onRefresh()
			},
			/**
			 * 详情
			 */
			onDetail(item) {
				storage.setLiveType(this.type)
				AppSocket.onBing()
				uni.navigateTo({
					url: "/live/pages/live/lookLive?live_id=" + item.id
				})
			},
			
			/**
			 * 筛选
			 */
			onScreen(index) {
				this.current_index = index
				this.onRefresh()
			},
			
			/**
			 * 刷新
			 */
			onRefresh() {
				this.page = 1
				this.list = []
				this.getLiveList()
			},
			/**
			 * 获取直播列表
			 */
			async getLiveList() {
				let res = await liveList({
					page: this.page,
					type: this.type,
					cate_id: this.category_id,
					follow: this.current_index
				})
				this.total = res.data.total
				if (this.page == 1) {
					this.list = res.data.data
				} else {
					this.list = [...this.list, ...res.data.data]
				}
			},
			/**
			 * 获取直播分类
			 */
			async getCategoryList() {
				let res = await liveCategoryList(this.type)
				console.log("直播分类", res);
				let oneArray = [{
					"name": "全部",
					"id": 0,
				}]
				this.category_list = [...oneArray, ...res.data]
			},
		}
	}
</script>

<style lang="scss" scoped>
	.screen-bg {
		width: 100%;
		overflow: hidden;
		display: flex;
		flex-wrap: wrap;
		
		.screen-item {
			flex: 1;
			height: 80rpx;
			line-height: 80rpx;
			text-align: center;
			color: #333;
			font-size: 30rpx;
		}
		
		.choose {
			color: $theme-color;
		}
	}
	
	.top-head {
		position: sticky;
		left: 0;
		top: 0;
		width: 100%;
		z-index: 999;
		box-sizing: border-box;
		background-color: white;
		
		.wrap-tab {
			width: 100%;
			height: 100rpx;
			box-sizing: border-box;
			padding: 20rpx 34rpx;
			overflow: hidden;
			display: flex;
			white-space: nowrap;
		
			.tab-item {
				margin-right: 45rpx;
				display: inline-block;
				height: 54rpx;
				line-height: 54rpx;
				border-bottom: 6rpx solid white;
				text-align: center;
				padding: 0 8rpx;
				color: #666666;
				font-size: 28rpx;
		
				&:last-child {
					margin-right: 0;
				}
			}
		
			.item-select {
				border-bottom-color: #1DAF81;
				color: #1DAF81;
			}
		}
	}
	.wrap {
		padding-top: 20rpx;
		padding-bottom: env(safe-area-inset-bottom);
		width: 100%;
		overflow: hidden;
	}
	
	.wrap-list {
		width: 100%;
		padding: 0 30rpx;
		overflow: hidden;
		box-sizing: border-box;
		display: flex;
		justify-content: space-between;
		flex-wrap: wrap;
		
		.item {
			width: 334rpx;
			height: 445rpx;
			border-radius: 10rpx;
			overflow: hidden;
			position: relative;
			z-index: 2;
			margin-bottom: 20rpx;
			
			.bg {
				width: 334rpx;
				height: 445rpx;
				position: absolute;
				left: 0;
				top: 0;
				z-index: 1;
			}
					
			.content {
				position: relative;
				z-index: 3;
				width: 334rpx;
				height: 445rpx;
				background-color: rgba(0, 0, 0, 0.2);
				
				.status-bg {
					position: absolute;
					left: 19rpx;
					top: 18rpx;
					height: 42rpx;
					z-index: 4;
					display: flex;
					align-items: center;
					padding: 0 16rpx;
					background-color: rgba(0, 0, 0, 0.5);
					border-radius: 5rpx;
					border: 1rpx solid white;
					
					.pic {
						width: 17rpx;
						height: 18rpx;
					}
					.status {
						margin-left: 8rpx;
						color: white;
						font-size: 22rpx;
					}
				}
				
				.bottom {
					position: absolute;
					left: 15rpx;
					bottom: 20rpx;
					width: 304rpx;
					z-index: 4;
					
					.title {
						color: white;
						font-size: 24rpx;
						display: -webkit-box;
						-webkit-box-orient: vertical;
						-webkit-line-clamp: 1;
						overflow: hidden;
						text-overflow: ellipsis;
					}
					.info {
						margin-top: 12rpx;
						width: 100%;
						overflow: hidden;
						display: flex;
						align-items: center;
						
						.avatar {
							width: 46rpx;
							height: 46rpx;
							border-radius: 50%;
							overflow: hidden;
						}
						.nickname {
							margin-left: 10rpx;
							flex: 1;
							margin-right: 10rpx;
							color: white;
							font-size: 22rpx;
							overflow: hidden;
							display: -webkit-box;
							-webkit-box-orient: vertical;
							-webkit-line-clamp: 1;
							overflow: hidden;
							text-overflow: ellipsis;
						}
						.num {
							color: white;
							font-size: 22rpx;
						}
					}
				}
				
			}
		}
	}
</style>